package simpledb;


import java.util.Iterator;
import java.util.Map;

/**
 *  added by baij
 */
public class StringGroupIterator extends AbstractDbIterator {


    Map<Integer , Integer >  map ;
    TupleDesc td ;
    Iterator<Map.Entry<Integer, Integer>> entries ;


    public StringGroupIterator(Map<Integer, Integer>  map  , TupleDesc td ) {

        this.map = map ;
        this.td = td ;
        entries = map.entrySet().iterator();
    }


    @Override
    protected Tuple readNext() throws DbException, TransactionAbortedException {

        if(entries.hasNext()) {
            Map.Entry<Integer, Integer> entry = entries.next();

            Tuple tup = new Tuple(td);
            tup.setField(0, new IntField(entry.getKey()) );
            tup.setField(1, new IntField(entry.getValue()) );

            return tup ;
        }
        return null;
    }

    @Override
    public void open() throws DbException, TransactionAbortedException {

    }

    @Override
    public void rewind() throws DbException, TransactionAbortedException {

        entries = map.entrySet().iterator();
    }

    @Override
    public TupleDesc getTupleDesc() {
        return this.td ;
    }



}
